/**
 * Created by JiaJia on 2019/5/21 0021.
 */
public class Code01 {
    /**
     * 饿汉式
     */
    public static class EagerSingleton{
        private final static EagerSingleton instance=new EagerSingleton();
        private EagerSingleton(){ }
        public static EagerSingleton getIsntance(){
            return instance;
        }
    }

    /**
     * 懒汉式
     */
    public static class LazySingleton{
        private static LazySingleton instance =null;
        private LazySingleton(){}
        synchronized public static LazySingleton getInstance(){
            if(instance==null){
                instance=new LazySingleton();
            }
            return instance;
        }
    }

    /**
     * 双重校验
     */
    public static class DoubleCheck{
        private volatile static DoubleCheck instance=null;
        private DoubleCheck(){}
        public static DoubleCheck getInstance(){
            if (instance==null){
                synchronized (DoubleCheck.class){
                    if (instance==null){
                        instance=new DoubleCheck();
                    }
                }
            }
            return instance;
        }
    }

    public static void main(String[] args) {
        System.out.println(EagerSingleton.getIsntance()==EagerSingleton.getIsntance());
        System.out.println(LazySingleton.getInstance()==LazySingleton.getInstance());
        System.out.println(DoubleCheck.getInstance()==DoubleCheck.getInstance());
    }
}